package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.Mapper.CategoryMapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategporyServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
//根据id删除分类

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {

        //思路:当前分类是否关联其他数据
        LambdaQueryWrapper<Dish> dqw = new LambdaQueryWrapper<>();
        dqw.eq(Dish::getCategoryId, id);//条件
        int count = dishService.count(dqw);


        //查询当前分类是否关联菜品
        if (count > 0) {
//已经关联抛出业务异常
            throw new CustomException("当前业务已关联菜品,不能删除");
        }

        //查询当前分类是否关联套餐
        LambdaQueryWrapper<Setmeal> sqw = new LambdaQueryWrapper<>();
        sqw.eq(Setmeal::getCategoryId, id);
        int count1 = setmealService.count();


        if (count1 > 0) {
            //已经关联 抛出业务异常
            throw  new CustomException("当前业务已关联套餐,不能删除");
        }
        super.removeById(id);
    }
}















